﻿using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Controls
{
    /// <summary>
    /// 组织机构摄像机树
    /// </summary>
    public partial class OrgCameraTree : UserControl
    {
        private ObservableCollection<TreeNodeData<PtOrgInfo>> _list;

        public OrgCameraTree()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载树节点
        /// </summary>
        public void LoadTree(ObservableCollection<TreeNodeData<PtOrgInfo>> list)
        {
            _list = list;
            this.orgTree.ItemsSource = list;
            loadingWait.Visibility = Visibility.Collapsed;
        }

        #region CheckBox 组织机构选择
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            TreeNodeData nodeData = checkBox.Tag as TreeNodeData;

            if (checkBox.IsChecked.Value)
            {
                CheckChildren(nodeData, true);
            }
            else
            {
                CheckParent(nodeData.Parent, false);
                CheckChildren(nodeData, false);
            }
        }

        private void CheckChildren(TreeNodeData nodeData, bool check)
        {
            if (nodeData.IsOrgLeaf)
            {
                nodeData.CameraNodeDataList.ForEach(item => item.IsSelected = check);
            }

            nodeData.Children.ToList().ForEach(child =>
            {
                child.IsSelected = check;
                CheckChildren(child, check);
            });
        }

        private void CheckParent(TreeNodeData parent, bool check)
        {
            if (parent != null)
            {
                parent.IsSelected = check;
                CheckParent(parent.Parent, check);
            }
        }
        #endregion

        #region CheckBox 摄像机选择
        private void Camera_CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            TreeNodeData nodeData = checkBox.Tag as TreeNodeData;

            if (checkBox.IsChecked.Value)
            {
            }
            else
            {
                CheckParent(nodeData.Parent, false);
            }
        }
        #endregion

        #region toggleButton 展开按钮事件
        private void toggleButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ToggleButton btn = sender as ToggleButton;
            btn.IsChecked = !btn.IsChecked;
            e.Handled = true;
        }

        private void toggleButton_Checked(object sender, RoutedEventArgs e)
        {
            ToggleButton btn = sender as ToggleButton;
        }
        #endregion

        #region pageCtrl_evtPageChanged 分页事件
        /// <summary>
        /// 分页事件
        /// </summary>
        private void pageCtrl_evtPageChanged(object sender, PageChangedEventArgs e)
        {
            SimplePageControl simplePageControl = sender as SimplePageControl;
            TreeNodeData<PtOrgInfo> nodeData = simplePageControl.Tag as TreeNodeData<PtOrgInfo>;
            List<TreeNodeData<PtCameraInfo>> list = GetCameraListPage(nodeData.CameraNodeDataList, nodeData.Info.id, e.PageNow, e.RecordsPerPage);
            nodeData.Children.Clear();
            list.ForEach(item =>
            {
                nodeData.Children.Add(item);
            });
        }
        #endregion

        #region GetCameraListPage 分页获取像机数据
        /// <summary>
        /// 分页获取像机数据
        /// </summary>
        private List<TreeNodeData<PtCameraInfo>> GetCameraListPage(List<TreeNodeData<PtCameraInfo>> cameraNodeDataList, long orgId, int currentPage, int pageSize)
        {
            List<TreeNodeData<PtCameraInfo>> result = new List<TreeNodeData<PtCameraInfo>>();

            int start = pageSize * (currentPage - 1);
            int end = pageSize * currentPage - 1;

            for (int i = start; i <= end; i++)
            {
                if (i < cameraNodeDataList.Count)
                {
                    result.Add(cameraNodeDataList[i]);
                }
            }

            return result;
        }
        #endregion

        #region textBlock_MouseLeftButtonDown 拖放
        private void textBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    //如果是录像回放，不加载拖拽事件
                    if (false)
                    {
                        return;
                    }
                    PtCameraInfo info = (sender as TextBlock).Tag as PtCameraInfo;
                    DragDrop.DoDragDrop(this, info, DragDropEffects.Copy);
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion

        #region menuShowDetails_Click 显示摄像机详细信息
        private void menuShowDetails_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            PtCameraInfo cameraInfo = menuItem.Tag as PtCameraInfo;
            MessageBox.Show(cameraInfo.CAMERA_NAME);
        }
        #endregion

        #region 获取选择的摄像机
        /// <summary>
        /// 获取选择的摄像机
        /// </summary>
        public List<PtCameraInfo> GetSelectedCameras()
        {
            List<PtCameraInfo> result = new List<PtCameraInfo>();

            foreach (TreeNodeData item in _list)
            {
                result.AddRange(GetSelectedCameras(item));
            }

            return result;
        }

        private List<PtCameraInfo> GetSelectedCameras(TreeNodeData nodeData)
        {
            List<PtCameraInfo> result = new List<PtCameraInfo>();

            if (nodeData.IsOrgLeaf)
            {
                foreach (TreeNodeData<PtCameraInfo> item in nodeData.CameraNodeDataList)
                {
                    if (item.IsSelected)
                    {
                        result.Add(item.Info);
                    }
                }
            }
            else
            {
                foreach (TreeNodeData item in nodeData.Children)
                {
                    result.AddRange(GetSelectedCameras(item));
                }
            }

            return result;
        }
        #endregion

        #region 获取选择的组织机构
        /// <summary>
        /// 获取选择的摄像机
        /// </summary>
        public List<PtOrgInfo> GetSelectedOrgs()
        {
            List<PtOrgInfo> result = new List<PtOrgInfo>();

            foreach (TreeNodeData item in _list)
            {
                result.AddRange(GetSelectedOrgs(item));
            }

            return result;
        }

        private List<PtOrgInfo> GetSelectedOrgs(TreeNodeData nodeData)
        {
            List<PtOrgInfo> result = new List<PtOrgInfo>();

            if (nodeData is TreeNodeData<PtOrgInfo>)
            {
                PtOrgInfo orgInfo = (nodeData as TreeNodeData<PtOrgInfo>).Info;
                if (nodeData.IsSelected)
                {
                    result.Add(orgInfo);
                }

                foreach (TreeNodeData item in nodeData.Children)
                {
                    result.AddRange(GetSelectedOrgs(item));
                }
            }

            return result;
        }
        #endregion

        #region 查询节点
        /// <summary>
        /// 查询节点
        /// </summary>
        public void Search(string text)
        {
            this.loadingWait.Visibility = Visibility.Visible;

            foreach (TreeNodeData item in _list)
            {
                ClearSearch(item); //清空查询结果
            }

            if (!string.IsNullOrWhiteSpace(text))
            {
                Search(text, null);
            }

            this.loadingWait.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 查询节点
        /// </summary>
        private bool Search(string text, TreeNodeData nodeData = null)
        {
            bool result = false; //是否查询到满足条件的节点

            if (nodeData == null)
            {
                foreach (TreeNodeData<PtOrgInfo> item in _list) //遍历根节点
                {
                    //item.NodeData.CameraCountVisible = Visibility.Collapsed; //隐藏像机数

                    bool bl = Search(text, item); //递归查询其子节点
                    if (item.Info.orgName.Contains(text)) //当前节点满足条件
                    {
                        item.IsVisible = true;
                    }
                    else
                    {
                        if (bl) //子节点查询到结果
                        {
                            item.IsVisible = true;
                        }
                        else //子节点没有查询到结果
                        {
                            item.IsVisible = false;
                        }
                    }
                }
            }
            else
            {
                //nodeData.CameraCountVisible = Visibility.Collapsed; //隐藏像机数

                if (nodeData.IsOrgLeaf)
                {
                    foreach (TreeNodeData<PtCameraInfo> item in nodeData.CameraNodeDataList)
                    {
                        if (item.Info.SHORT_MSG != null && item.Info.SHORT_MSG.Contains(text)) //像机节点满足条件
                        {
                            nodeData.SearchMode = true; //隐藏分页控件
                            item.IsVisible = true; //显示
                            bool exists = false;
                            foreach (TreeNodeData<PtCameraInfo> child in item.Parent.Children)
                            {
                                if (child.Info.ID == item.Info.ID)
                                {
                                    exists = true;
                                    break;
                                }
                            }
                            if (!exists)
                            {
                                item.Parent.Children.Add(item);
                            }
                            result = true;
                        }
                        else
                        {
                            item.IsVisible = false; //不满足查询条件不显示
                        }
                    }
                }
                else
                {
                    foreach (TreeNodeData<PtOrgInfo> item in nodeData.Children)
                    {
                        bool bl = Search(text, item); //递归查询其子节点
                        if (item.Info.orgName.Contains(text)) //当前节点满足条件
                        {
                            item.IsVisible = true;
                            result = true;
                        }
                        else
                        {
                            if (bl) //子节点查询到结果
                            {
                                item.IsVisible = true;
                                result = true;
                            }
                            else //子节点没有查询到结果
                            {
                                item.IsVisible = false;
                            }
                        }
                    }
                }
            }

            return result;
        }
        #endregion

        #region 清空查询
        /// <summary>
        /// 清空查询
        /// </summary>
        private void ClearSearch(TreeNodeData nodeData)
        {
            nodeData.SearchMode = false; //显示分页控件
            nodeData.IsVisible = true;
            if (nodeData.CameraNodeDataList != null && nodeData.CameraNodeDataList.Count > 0)
            {
                foreach (TreeNodeData<PtCameraInfo> child in nodeData.CameraNodeDataList)
                {
                    child.IsVisible = true;
                }

                //分页处理
                List<TreeNodeData<PtCameraInfo>> list = GetCameraListPage(nodeData.CameraNodeDataList, (nodeData as TreeNodeData<PtOrgInfo>).Info.id, 1, nodeData.PageSize);
                nodeData.Children.Clear();
                list.ForEach(item =>
                {
                    nodeData.Children.Add(item);
                });
            }
            else
            {
                foreach (TreeNodeData child in nodeData.Children)
                {
                    ClearSearch(child);
                }
            }
        }
        #endregion

    }
}
